//
//  WXQueryDetailViewController.m
//  CarJX
//
//  Created by jxcl on 14-11-17.
//  Copyright (c) 2014年 KK. All rights reserved.
//

#import "WXQueryDetailViewController.h"
#import "TFHpple.h"
#import "UIViewController+HttpRequest.h"
#import "LoginViewController.h"
#import "WXContactShopViewController.h"
#import <AFNetworking.h>
@interface WXQueryDetailViewController ()<UIAlertViewDelegate>
{
    NSString *_imageStr;
}
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@property (weak, nonatomic) IBOutlet UIButton *contectBtn;
@property (weak, nonatomic) IBOutlet UILabel *errorLabel;
- (IBAction)contectShop:(UIButton *)sender;

@end

@implementation WXQueryDetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

/*
 http://jx.jxhld.gov.cn:20002/info/carWf.do?hpno=%E8%B5%A3&hphm=A16258&hpzl=02&clsbdh=493823
 
 hpno//车牌简称
 hphm//车牌号
 hpzl //车辆类型   小型汽车02 顺序的
 clsbdh //车架号
*/

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setTitle:@"违章详情"];
    if (![Common currenLoginStatus]) {
        self.contectBtn.enabled = NO;
        self.contectBtn.backgroundColor = [UIColor lightGrayColor];
    }
/*
    NSString *hpno = @"赣";
    NSString *hphm = @"A16258";
    NSString *hpzl = @"02";
    NSString *clsbdh = @"493823";
 */
    
    NSString *base = @"http://jx.jxhld.gov.cn:20002/info/carWf.do";
    NSString *imageBase = @"http://jx.jxhld.gov.cn:20002";
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];  //设置请求头
    [self.view makeToastActivity];
    [manager POST:base parameters:@{@"hpno": _hpno,@"hphm": _hphm ,@"hpzl":_hpzl ,@"clsbdh":_zhengShuNo} success:^(AFHTTPRequestOperation *operation, id responseObject) {
         [self.view hideToastActivity];
        TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:responseObject];   //返回的是html数据 进行解析
        NSArray *elements  = [xpathParser searchWithXPathQuery:@"//img"];
        NSArray *elements2  = [xpathParser searchWithXPathQuery:@"//td[@class='ltd1']"];
        if (elements2.count >0) {
            TFHppleElement *temparr2 = elements2[0];
            NSString *resultStr = [temparr2 content];
            self.webView.hidden = YES;
            self.errorLabel.text = resultStr;
            self.contectBtn.enabled = NO;
            self.contectBtn.backgroundColor = [UIColor lightGrayColor];
           
            return ;

        }
       
        if (elements.count >= 1) {
            NSDictionary *temparr = nil;
            for (TFHppleElement *element in elements) {
                NSLog(@"%@",element );
                if ([element attributes]!=nil) {
                    temparr = [element attributes];
                }
            }
            NSString *image = [temparr[@"src"] substringFromIndex:2];
            _imageStr = [imageBase stringByAppendingString:image];
            [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:_imageStr]]];
            self.contectBtn.enabled = YES;
            self.contectBtn.backgroundColor = [UIColor redColor];
            int queryNum = [[[NSUserDefaults standardUserDefaults] objectForKey:@"queryNum"] intValue];  //限制查询次数
            queryNum ++;
            [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:queryNum] forKey:@"queryNum"];
        }else{
            NSString *errorStr = nil;
            elements = [xpathParser searchWithXPathQuery:@"//label"];
            for (TFHppleElement *element in elements) {
                NSLog(@"%@",element );
                if ([element content]!=nil) {
                    errorStr = [element content];
                }
            }
            self.webView.hidden = YES;
            self.errorLabel.text = errorStr;
            self.contectBtn.enabled = NO;
            self.contectBtn.backgroundColor = [UIColor lightGrayColor];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self.view hideToastActivity];
        [self.view makeToast:@"请检查网络连接,并重试"];
        NSLog(@"%@",operation.response.URL);
        NSLog(@"%@",error.userInfo);
        self.contectBtn.enabled = NO;
        self.contectBtn.backgroundColor = [UIColor lightGrayColor];
    }];
 
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)contectShop:(UIButton *)sender {
   
    if (![Common currentUserId]) {
        LoginViewController *login = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"LoginView"];
        CLUIBaseNavigationController *base = [[CLUIBaseNavigationController alloc] initWithRootViewController:login];
        login.hidesBottomBarWhenPushed = YES;
        [self presentViewController:base animated:YES completion:^{
            
        }];
    } else {
     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示"message:@"你确定需要代办吗？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
     [alertView show];
    }
 //    WXContactShopViewController *contact =  [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"contact"];
//    contact.seletedTraffic = [_violationDic copy];
//    WXBaseNavigationController *base = [[WXBaseNavigationController alloc] initWithRootViewController:contact];
//    contact.identity = @"网上获取";
//    contact.imageURl = _imageStr;
//    [self.navigationController pushViewController:contact animated:YES];
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
        [self.view makeToastActivity];
        [self GETWithContentType:@"imibreakrual" params:@{@"member_id" :[Common currentUserId] , @"seller_id" : @"" ,@"imgurl":@""} success:^(ResultMy *result) {
            [self.navigationController.view makeToast:@"亲！您的代办已经受理。" duration:1.8f position:TOP_POINT];
            [self.navigationController popViewControllerAnimated:YES];
            [self.view hideToastActivity];
        } failure:^(NSError *error) {
            [self.view makeToast:@"请检查网络" duration:1.8f position:TOP_POINT];
            [self.view hideToastActivity];
        }];
    }
}


@end
